package com.kdtech.analyse.Bbs;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.NumberUtils;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.RegexUtils;

public class _0768wBbsAnalyse implements AnalyseNews {

	
	public boolean isDetailPage(String url) {
		String[] regex = {
				"http://www.0768w.com/forum/thread-[0-9]*-1-1.html",
		};
		return RegexUtils.matchAny(url, regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta bbs = new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt = urlMeta.getHtml();
		String url = urlMeta.getUrl();

		bbs.setUrl(url);
		String title = null;
		String content = null;
		Long date = null;

		Document doc = Jsoup.parse(htmltxt);
		doc.select("div.topic_title_l h1 b").remove();
		title = doc.select("div.topic_title_l h1").text().replace(" ", "");
		date = DateUtils.matchDate(doc.select("div.menber ul li:eq(2)").text());
		if(date == null)
			date = DateUtils.matchDate(doc.select(".menber").text());
		content = HtmlCleaner.getContentHtml(url,  doc.select("td.topic_c"));

		/**
		 * 初始化、截取 、设置作者信息*******************************************
		 *
		 * */
		String author = null;
		String str = null;
		str = doc.select(".menber").text();
		str = StringUtils.substringBefore(str, "关注此帖");
		if(doc.select(".topic_name").size() >= 1)
			author = doc.select(".topic_name").get(0).text();
		if(StringUtils.isBlank(author)){
			author = StringUtils.substringBetween(str, "作者：", "阅读");
		}
		String clickNum = doc.select("div.menber_user ul li:eq(1)").text().replace("阅读：", "");
		String commnetNum = doc.select("div.menber_user ul li:eq(2)").text().replace("回复：", "");
		
		bbs.setAuthor(author);
		/**
		 *                        	 **************************end *************************
		 *
		 **/
		bbs.setTitle(title);
		bbs.setContent(content);
		bbs.setClickNum(NumberUtils.parseInt(clickNum));
		bbs.setCommentNum(NumberUtils.parseInt(commnetNum));
		bbs.setDate(date);
		return bbs;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	public static void main(String[] args) {
		_0768wBbsAnalyse a = new _0768wBbsAnalyse();

		String url = "http://www.0768w.com/forum/thread-3338521-1-1.html";
		System.out.println(DoMainUtils.GetDomainName(url));
		if (!a.isDetailPage(url)) {
			System.out.println("不匹配规则");
		} else {
			UrlMeta meta = CrawlHTML.responseToURL(url);
			NewsMeta parserHtml = a.parserHtml(meta);
			System.out.println(parserHtml);
//			System.out.println(a.Update(parserHtml));
		}

	}
	
}
